package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public void setSetmealStatus(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            List<SetmealDish> dishList = setmealDishMapper.selectStatus(id);
            // stream流办法
            List<Long> ids = dishList.stream().map(setmealDish -> {
                return setmealDish.getDishId();
            }).collect(Collectors.toList());
            // 传统办法
//            List<Long> ids = new ArrayList<>();
//            for (SetmealDish setmealDish : dishList) {
//                Long dishId = setmealDish.getDishId();
//                ids.add(dishId);
//            }
            // 根据ids查询所有菜品
            for (Long dishId : ids) {
                Dish dish = dishMapper.selectDishById(dishId);
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.updateSetmealList(setmeal);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO findSetmealById(Long id) {
        // 查询套餐表
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
        // 查询套餐菜品关系表
        List<SetmealDish> list = setmealMapper.selectSetmealDishById(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 更新套餐信息
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmealList(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 更新套餐
        setmealMapper.updateSetmealList(setmeal);

        // 先删除
        setmealMapper.deleteSetmealById(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        // 再添加
        setmealDishMapper.insertSetmealDish(setmealDishes);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);
        log.info("setmealDTO", setmealDTO);

        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        // 将菜品跟套餐关联
        setmealDishMapper.insertSetmealDish(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        log.info("[分页] setmealPageQueryDTO", setmealPageQueryDTO);

        Page<SetmealVO> page = setmealMapper.pageSetmeal(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.findSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        for (Long sid : ids) {
            setmealMapper.deleteSetmealById(sid);
            setmealDishMapper.deletesetmealDishById(sid);
        }
    }
}
